package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Avatar;
import javafx.scene.image.Image;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class AvatarActions {
    static{
        PropertyFactory.regist(Avatar.class, "imageProperty", o -> {
            Avatar obj = (Avatar) o;
            return obj.imageProperty();
        });
        PropertyFactory.regist(Avatar.class, "radiusProperty", o -> {
            Avatar obj = (Avatar) o;
            return obj.radiusProperty();
        });
    }

    public static void init(Avatar node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        Image image = JavaFXUtils.getImage(thing, "image", actionContext);
        if(image != null){
            node.setImage(image);
        }

        if(thing.valueExists("radius")){
            node.setRadius(thing.getDouble("radius"));
        }
    }

    public static Avatar create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Avatar node = new Avatar();
        init(node, self, actionContext);

        actionContext.g().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }
}
